let App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 快捷导航
    nav_select: false,
    // 当前页面参数
    options: {},
    exist_address: false, // 是否存在收货地址
    showImages: [],
    is_showImages:false,
    // 禁用submit按钮
    disabled: false,
    hasError: false,
    error: '',
    user_id:'',
    sex:'',
    page_sex:'',
    pair_type:'my',
    first_day:'',
    last_day:'',
    day: '',
    time: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 当前页面参数
    this.setData({
      pair_id: options.pair_id,
      pair_type: options.pair_type ? options.pair_type:'my',
      status: options.status
    })
    console.log(this.data.status);
    //根目录路径
    this.setData({
      app_root: App.root
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取当前订单信息
    //this.getOrderData();
    //获取当前用户信息
    //this.getUserDetail();
    //获取信息
    this.getDetail();
  },

  /**
   * 获取相亲信息
   */
  getDetail:function(){
    let _this = this;
    let pair_type = _this.data.pair_type;
    let pair_id = _this.data.pair_id;
    App._get('pair/getDetail', { pair_id: pair_id, pair_type: pair_type}, function (result) {
      console.log(result);
      let showImagesArr = [];
      if (result.data.detail.images) {
        for (var j = 0; j < result.data.detail.images.length; j++) {
          showImagesArr.push(App.root + result.data.detail.images[j]);
        }
      }
      _this.setData(result.data);
      _this.setData({
        showImages: showImagesArr
      })
      _this.setData({
        sex: result.data.detail.sex,
        first_day:result.data.week.first_day,
        last_day:result.data.week.last_day,
        day:result.data.week.day,
        time:result.data.time
      })

    });
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail: function () {
    let _this = this;
    let user_id = _this.data.user_id;
    App._get('user.index/detail', {user_id:user_id}, function (result) {
      //console.log(result.data);
      _this.setData(result.data);
    });
  },


  //放大图片查看
  previewImage: function (e) {
    let key = e.target.dataset.id;
    let imgSrc = this.data.showImages;

    wx.previewImage({
      current: imgSrc[key],
      urls: imgSrc
    })
  },

  //展示或关闭 图片
  is_showImages:function(){
    let that = this;
    if (that.data.is_showImages){
      that.setData({
        is_showImages: false
      });
    }else{
      that.setData({
        is_showImages: true
      });
    }
    console.log(that.data.is_showImages);
  },


  dump_detail: function (e) {
    let coupon_id = e.currentTarget.dataset.coupon_id;
    wx.navigateTo({
      url: '/pages/coupon/ren_detail?coupon_id=' + coupon_id +'&status_type=pair'
    })
  },

  /**
   * 确认购买弹窗
   */
  onToggleTrade(e) {
    if (typeof e === 'object') {
      // 记录formId
      e.detail.hasOwnProperty('formId') && App.saveFormId(e.detail.formId);
    }
    this.setData({
      showBottomPopup: !this.data.showBottomPopup
    });
  },

  //查看任务
  goToContent:function(e){
    let task_id = e.currentTarget.dataset.task_id;
    wx.navigateTo({
      url: '/pages/social/content?type=task&id=' + task_id
    })
  },

  //购买
  useCoupon: function (e) {
    let coupon_id = this.data.detail.coupon_id;
    let status = 'pair';
    let pair_id = this.data.pair_id;

    // 订单创建成功后回调--微信支付
    let callback = function (result) {
      if (result.code === -10) {
        App.showError(result.msg, function () {
          // 跳转到未付款订单
          wx.redirectTo({
            url: '/pages/doing/user/index',
          });
        });
        return false;
      }
      // 发起微信支付
      wx.requestPayment({
        timeStamp: result.data.payment.timeStamp,
        nonceStr: result.data.payment.nonceStr,
        package: 'prepay_id=' + result.data.payment.prepay_id,
        signType: 'MD5',
        paySign: result.data.payment.paySign,
        success: function (res) {

          // 跳转到订单详情
          wx.redirectTo({
            //url: '../order/detail?order_id=' + result.data.order_id,
            url: '/pages/doing/user/index?type=userGather',
          });
        },
        fail: function () {
          App.showError('订单未支付', function () {
            // 跳转到未付款订单
            wx.redirectTo({
              url: '/pages/doing/user/index?type=userGather',
            });
          });
        },
      });
    };

    wx.showModal({
      title: '提示',
      content: '您确定要购买吗？',
      success(res) {
        if (res.confirm) {
          App._get('Doing/buy_pair', {
            coupon_id: coupon_id,
            status: status,
            pair_id: pair_id
          }, function (result) {
            //console.log(result);
            //未登录
            if (result.data.code == 2) {
              App.showIsLogin(result.msg);
            }
            if (result.code == 1) {
              callback(result);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  //邀请
  invite:function(e){
    let coupon_id = this.data.detail.coupon_id;
    let status = 'pair';
    let pair_id = this.data.pair_id;
    let day = this.data.day;
    let time = this.data.time;

    App._get('Doing/confirm_pair', {
      coupon_id: coupon_id,
      status: status,
      pair_id: pair_id,
      day:day,
      time:time
    }, function (result) {
      // console.log(result);
      //未登录
      if (result.data.code == 2) {
        App.showIsLogin(result.msg);
      }
      if (result.code == 1) {
        App.showError(result.msg, function () {
          // 跳转到订单详情
          wx.redirectTo({
            url: '/pages/doing/user/index?type='+result.data.type,
          });
        });
      }
    });
  },


  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      day: e.detail.value
    })
  },
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  goBack: function () {
    wx.navigateBack();
  },

});